package com.example.hbase.handler.ext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.hbase.handler.AbstractTypeHandler;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.List;

/**
 * @author void
 * @date 2021/11/26 16:22
 * @desc list映射器
 */
public class ListHandler extends AbstractTypeHandler {
    @Override
    protected boolean aboutToHandle(Class<?> type) {
        return type == List.class || type == JSONArray.class;
    }

    @Override
    protected byte[] innerToBytes(Class<?> type, Object value) {
        String str = JSON.toJSONString(value);
        return Bytes.toBytes(str);
    }

    @Override
    protected Object innerToObject(Class<?> type, byte[] bytes) {
        String str = Bytes.toString(bytes);
        if(StringUtils.isBlank(str)){
            //避免空字符串
            return null;
        }
        return JSON.parseArray(str);
    }
}
